unit ufrmUser_Param_Settings;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ufrmChildMasterDetail, cxTL,
  cxEdit, DB, cxDBData, DBClient,
  Provider, ADODB, DosMove, Menus, ExtCtrls, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, cxInplaceContainer, cxTLData, cxDBTL, cxPC,
  StdCtrls, RzPanel, RzSplit, Mask, DBCtrls, cxButtonEdit, uPubDeclare,
  cxGraphics, cxCustomData, cxStyles, cxFilter, cxData, cxDataStorage;

type
  TfrmUser_Param_Settings = class(TfrmChildMasterDetail)
    adsMastUSER_ID: TLargeintField;
    adsMastUSER_NO: TStringField;
    adsMastUSER_NM: TStringField;
    adsMastPASS_WORD: TStringField;
    adsMastIS_ADMIN: TStringField;
    adsMastDISABLE: TStringField;
    adsMastEMAIL: TStringField;
    adsMastTEL: TStringField;
    adsMastADD_USER: TLargeintField;
    adsMastADD_TIME: TDateTimeField;
    adsMastEDIT_USER: TLargeintField;
    adsMastEDIT_TIME: TDateTimeField;
    cdsMastUSER_ID: TLargeintField;
    cdsMastUSER_NO: TStringField;
    cdsMastUSER_NM: TStringField;
    cdsMastPASS_WORD: TStringField;
    cdsMastIS_ADMIN: TStringField;
    cdsMastDISABLE: TStringField;
    cdsMastEMAIL: TStringField;
    cdsMastTEL: TStringField;
    cdsMastADD_USER: TLargeintField;
    cdsMastADD_TIME: TDateTimeField;
    cdsMastEDIT_USER: TLargeintField;
    cdsMastEDIT_TIME: TDateTimeField;
    adsDetailUPS_ID: TLargeintField;
    adsDetailUSER_ID: TLargeintField;
    adsDetailSP_ID: TLargeintField;
    cdsDetailUPS_ID: TLargeintField;
    cdsDetailUSER_ID: TLargeintField;
    cdsDetailSP_ID: TLargeintField;
    cxgrdbtblvwDetail: TcxGridDBTableView;
    cxgrdlvlDetail: TcxGridLevel;
    cxgrdDetail: TcxGrid;
    lblUSER_NO: TLabel;
    dbedtUSER_NO: TDBEdit;
    lblUSER_NM: TLabel;
    dbedtUSER_NM: TDBEdit;
    lblEMAIL: TLabel;
    dbedtEMAIL: TDBEdit;
    lblTEL: TLabel;
    dbedtTEL: TDBEdit;
    adsDetailSP_NAME: TStringField;
    cdsDetailSP_NAME: TStringField;
    cxgrdbclmnDetailUPS_ID: TcxGridDBColumn;
    cxgrdbclmnDetailUSER_ID: TcxGridDBColumn;
    cxgrdbclmnDetailSP_ID: TcxGridDBColumn;
    cxgrdbclmnDetailP_VALUE: TcxGridDBColumn;
    cxgrdbclmnDetailSP_NAME: TcxGridDBColumn;
    adsDetailTABLE_NAME: TStringField;
    cdsDetailTABLE_NAME: TStringField;
    adsDetailVALUE_COLUMN: TStringField;
    cdsDetailVALUE_COLUMN: TStringField;
    cxgrdbclmnTreeUSER_ID: TcxGridDBColumn;
    cxgrdbclmnTreeUSER_NO: TcxGridDBColumn;
    cxgrdbclmnTreeUSER_NM: TcxGridDBColumn;
    adsDetailDISP_COLUMNS: TStringField;
    adsDetailWIDTH_COLUMNS: TStringField;
    cdsDetailDISP_COLUMNS: TStringField;
    cdsDetailWIDTH_COLUMNS: TStringField;
    adsDetailP_VALUE: TStringField;
    cdsDetailP_VALUE: TStringField;
    adsDetailD_VALUE: TStringField;
    adsDetailD_VALUE_NAME: TStringField;
    cdsDetailD_VALUE: TStringField;
    cdsDetailD_VALUE_NAME: TStringField;
    cxgrdbclmnDetailD_VALUE: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure cxgrdbclmnDetailVALUEPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure dspDetailGetTableName(Sender: TObject; DataSet: TDataSet;
      var TableName: String);
  private
    { Private declarations }
    procedure RefDetail;
  protected
    procedure doQuery; override;

    procedure doAfterOperate(Operate: TOperateState); override;
    procedure doBeforeOperate(Operate: TOperateState); override;
  public
    { Public declarations }
    function CanOperate(AOperate: TOperateState): Boolean; override;
  end;

var
  frmUser_Param_Settings: TfrmUser_Param_Settings;

implementation

uses uPubFuncs;

{$R *.dfm}

{ TfrmUser_Param_Settings }

procedure TfrmUser_Param_Settings.RefDetail;
var
  sSql: string;
begin
  if not Assigned(cdsMast) then Exit;
  if not cdsMast.Active then Exit;
  if (cdsMastUSER_ID.AsString = '') then Exit;
  if cdsDetail.IsEmpty or (cdsDetailUSER_ID.AsString <> cdsMastUSER_ID.AsString) then
  begin
    sSql :=
      'merge into user_param_settings t ' +
      'using sys_params a on (t.user_id = '+cdsMastUSER_ID.AsString+' and a.sp_id = t.sp_id) ' +
      ' when not matched then ' +
      '      insert (ups_id, user_id, sp_id) ' +
      '      values (gen_id(gen_user_param_settings, 1), '+cdsMastUSER_ID.AsString+', a.sp_id) ';
    ExcuteSql(sSql);
    cdsDetail.Refresh;
  end;
end;          

procedure TfrmUser_Param_Settings.FormCreate(Sender: TObject);
begin
  inherited;
  ExApp.AddIdleMethod(RefDetail);
end;

procedure TfrmUser_Param_Settings.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin  
  ExApp.RemoveIdleMethod(RefDetail);
  inherited;
end;

procedure TfrmUser_Param_Settings.doAfterOperate(Operate: TOperateState);
begin
  inherited;
  SetReadOnly(dbedtUSER_NO);
  SetReadOnly(dbedtUSER_NM);
  SetReadOnly(dbedtTEL);
  SetReadOnly(dbedtEMAIL);
  SetReadOnly(cxgrdbclmnDetailSP_NAME);
  SetWriteable(cxgrdbclmnDetailP_VALUE);
  SetReadOnly(cxgrdbclmnDetailD_VALUE);
end;

procedure TfrmUser_Param_Settings.doBeforeOperate(Operate: TOperateState);
begin
  inherited;

end;

function TfrmUser_Param_Settings.CanOperate(
  AOperate: TOperateState): Boolean;
begin
  case Focusing of
    fMaster: Result := not (AOperate in [mAdd, mEdit, mDelete]);
    fDetail: Result := not (AOperate in [mAdd, mDelete]);
  end;
end;

procedure TfrmUser_Param_Settings.doQuery;
begin
  inherited;
  if cdsMast.Active then cdsMast.Close;
  cdsMast.Params.ParamByName('search_str').AsString := edtSearch.Text;
  cdsMast.Open;
  if not cdsDetail.Active then cdsDetail.Open;
end;

procedure TfrmUser_Param_Settings.cxgrdbclmnDetailVALUEPropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
var
  vLst: TStrings;
begin
  inherited;
  if not (OperateState in [mAdd, mEdit]) then Exit;
  vLst := TStringList.Create;
  try
    vLst.Values['SQL'] := MyFormatStr('select * from %s', [cdsDetailTABLE_NAME.AsString]);
    vLst.Values['KeyFields'] := cdsDetailVALUE_COLUMN.AsString;
    vLst.Values['Label'] := cdsDetailDISP_COLUMNS.AsString;
    vLst.Values['Width'] := cdsDetailWIDTH_COLUMNS.AsString;
    if DoSelect(vLst) then
    begin
      if not (cdsDetail.State in [dsInsert, dsEdit]) then cdsDetail.Edit;
      cdsDetailP_VALUE.AsString := vLst.Values[cdsDetailVALUE_COLUMN.AsString];
      cdsDetailD_VALUE.AsString := vLst.Values[cdsDetailD_VALUE_NAME.AsString];
    end;
  finally
    FreeAndNil(vLst);
  end;
end;

procedure TfrmUser_Param_Settings.dspDetailGetTableName(Sender: TObject;
  DataSet: TDataSet; var TableName: String);
begin
  inherited;
  TableName := 'USER_PARAM_SETTINGS';
end;

initialization
  RegisterClass(TfrmUser_Param_Settings);

end.
